package com.banzhi.shop.entity.HomeGridView;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Icon;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.activity.MainActivity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/28.
 */

public class GridViewAdapter extends ArrayAdapter<IconBean> {
    private Context mContext;
    private int layoutResourceId;
    private ArrayList<IconBean> mGridData = new ArrayList<IconBean>();

    public GridViewAdapter(Context context, int resource, ArrayList<IconBean> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.layoutResourceId = resource;
        this.mGridData = objects;
    }


    public void setGridData(ArrayList<IconBean> mGridData) {
        this.mGridData = mGridData;
        notifyDataSetChanged();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.txt_icon);
            holder.imageView = (ImageView) convertView.findViewById(R.id.img_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        IconBean item = mGridData.get(position);
        holder.textView.setText(item.getName());
        Picasso.with(mContext).load(item.getAdvimg()).placeholder(R.mipmap.grid_item).into(holder.imageView);

        return convertView;
    }

    private class ViewHolder {
        TextView textView;
        ImageView imageView;
    }


}
